package com.example.hdwx.myapplication.HomeActivity.goodchoice_fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.hdwx.myapplication.R;
import com.example.hdwx.myapplication.adapter.Goodchoice_Fragment_Adapter;
import com.example.hdwx.myapplication.eneity.Api;
import com.example.hdwx.myapplication.eneity.goodChoice_eneitys.CyclicGraphEneity;
import com.example.hdwx.myapplication.json.GoodChoiceJson;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import static android.content.ContentValues.TAG;

/**
 * Created by Administrator on 2017/2/12.
 * 优选碎片
 */

public class Goodchoice_Fragment extends Fragment {

    ImageView imageView;
    SwipeRefreshLayout refresh;
    RecyclerView recyclerView;
    List<CyclicGraphEneity> list;
    LinearLayoutManager manager;
    private Goodchoice_Fragment_Adapter adapter;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.goodchoice_fragment,container,false);
        imageView = (ImageView) view.findViewById(R.id.goodChoice_imageView);
        refresh = (SwipeRefreshLayout) view.findViewById(R.id.goodChoice_swipeRefreshLayout);
        recyclerView = (RecyclerView) view.findViewById(R.id.goodChoice_recyclerView);
        return  view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //设置并绑定布局管理器
        manager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);

        getData();
        refresh();
    }


    //获取网络数据
    public void getData() {

//        if (list.size()!=0){
//            adapter = new Goodchoice_Fragment_Adapter(getContext(), imageView, list);
//            recyclerView.setAdapter(adapter);
//        }else {
            OkHttpUtils.get().url(Api.GOODCHOICE_NEWEST).build().execute(new StringCallback() {
                @Override//数据请求失败时调用
                public void onError(Request request, Exception e) {
                    Toast.makeText(getContext(), "数据请求失败...", Toast.LENGTH_SHORT).show();
                    getData();
                }

                @Override//数据请求成功时调用
                public void onResponse(final String response) {
                    list = GoodChoiceJson.getContent(response,0,10);
                    adapter = new Goodchoice_Fragment_Adapter(getContext(), imageView, list);
                    recyclerView.setAdapter(adapter);

                    //上滑加载数据
                    recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
                        int start = 0;
                        int end = 20;
                        int itemId = 7;
                        @Override
                        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                            super.onScrolled(recyclerView, dx, dy);
                            int lastVisibleItem = ((LinearLayoutManager)recyclerView.getLayoutManager()).findLastVisibleItemPosition();
                            int totalItemCount = recyclerView.getLayoutManager().getItemCount();
                            //lastVisibleItem >= totalItemCount - 3 表示剩下3个item自动加载，各位自由选择
                            // dy>0 表示向下滑动
                            if (lastVisibleItem >= totalItemCount - 3 && dy > 0) {
                                list = GoodChoiceJson.getContent(response,start,end);
                                end += 10;
                                //刷新适配器数据
                                adapter.notifyDataSetChanged();
                                adapter = new Goodchoice_Fragment_Adapter(getContext(), imageView, list);
                                    //实现item定位
                                    manager.scrollToPositionWithOffset(itemId, 0);
                                    manager.setStackFromEnd(true);
                                itemId += 10;
                                recyclerView.setAdapter(adapter);
                            }
                        }
                    });

                }
            });
//        }
    }

    public void refresh(){
        //设置刷新圆环颜色
        refresh.setColorSchemeColors(Color.YELLOW);
        //下拉刷新的具体操作
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //清空数据
                //重新加载数据
                getData();
                refresh.setRefreshing(false);
            }
        });
    }

}
